package com.its.feign.server.domain.d1.web;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.its.common.model.WebResult;
import com.its.feign.server.domain.d1.model.entity.DtJson;
import com.its.feign.server.domain.d1.service.DtJsonService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* 前端控制器
* @author xiaxp
* @since 2021-05-20
*/
@Controller
@RequestMapping("/dtJson")
@Slf4j
public class DtJsonController {
    @Autowired
    private DtJsonService dtJsonService;

    /**
    * 条件查询
    */
    @GetMapping(value = "allData")
    @ResponseBody
    public WebResult allData(@RequestParam(required = false) String id) {
        DtJson query = new DtJson();
        if(StringUtils.isNotBlank(id)){
            query.setId(id);
        }
        Wrapper<DtJson> queryWrapper = new QueryWrapper<DtJson>(query);
        List<DtJson> list = dtJsonService.list(queryWrapper);
        return WebResult.success().put("list", list);
    }

    /**
    * 条件分页查询
    */
    @GetMapping(value = "pageData")
    @ResponseBody
    public WebResult pageData(@RequestParam int page, @RequestParam int limit,
                              @RequestParam(required = false) String orderBy, @RequestParam(required = false) Boolean isAsc){
        DtJson query = new DtJson();
        QueryWrapper<DtJson> queryWrapper = new QueryWrapper<DtJson>(query);
        if(StringUtils.isNotBlank(orderBy) && null != isAsc){
            if(isAsc){
                queryWrapper.orderByAsc(orderBy);
            }else{
                queryWrapper.orderByDesc(orderBy);
            }
        }
        IPage<DtJson> pageQuery = new Page<>(page, limit);
        Page<DtJson> pageInfo = (Page<DtJson>) dtJsonService.page(pageQuery,queryWrapper);
        return WebResult.success().put("page", pageInfo);
    }

    /**
     * 根据Name查询
     */
    @GetMapping(value = "name/{name}")
    @ResponseBody
    public WebResult getOneByNameLike(@PathVariable String name) {
        DtJson result = null;
        if(StringUtils.isNotBlank(name)){
            result = dtJsonService.getOneByNameLike(name);
        }
        return WebResult.success().put("detail", result);
    }

    /**
    * 根据ID查询
    */
    @GetMapping(value = "detail/{id}")
    @ResponseBody
    public WebResult detail(@PathVariable String id) {
        DtJson result = null;
        if(StringUtils.isNotBlank(id)){
            DtJson query = new DtJson();
            query.setId(id);
            result = dtJsonService.getById(id);
        }
        return WebResult.success().put("detail", result);
    }

    /**
    * 保存（新增或根据ID更新）
    */
    @PostMapping(value = "save")
    @ResponseBody
    public WebResult save(@RequestBody DtJson entity) {
        if(null == entity){
            return WebResult.error("保存对象不能为空");
        }
        boolean result = dtJsonService.saveOrUpdate(entity);
        return (result == true) ? WebResult.success() : WebResult.error("保存错误");
    }

    /**
    * 物理删除
    * @param id
    * @return
    */
    @PostMapping(value = "purge/{id}")
    @ResponseBody
    public WebResult purge(@PathVariable String id) {
        if(StringUtils.isBlank(id)){
            return WebResult.error("ID不能为空");
        }
        boolean rslt = dtJsonService.removeById(id);
        return (rslt == true) ? WebResult.success() : WebResult.error("删除失败");
    }
}
